﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Dynamics;

namespace Kosmos
{
    public class Particule : AbstractBall
    {

        private const String _path_particule = "Graphics\\particule";
        static private Texture2D _txt2d_particule;
        static private SpriteBatch _sprite;
        public const int _time_life = 800;//durée de vie
        private bool _isDead;

        private int _time_elapsed;

        protected Type _type_ball;


        public Particule(Game g, Vector2 position, float diameter)
            : base(g)
        {
            _isDead = false;
            _time_elapsed = 0;
            Body body =
                BodyFactory.Instance.CreateCircleBody
                (((MyGame)g).Simulator, diameter / 2f, 10);
            body.Position = position;
            body.IsStatic = false;
            body.LinearDragCoefficient = 0f;

            Geom = GeomFactory.Instance.CreateCircleGeom
                (((MyGame)g).Simulator, body, diameter / 2f, 100, 1);

            Geom.CollisionCategories = getType(Type.PARTICULE);
            Geom.CollidesWith = FarseerGames.FarseerPhysics.CollisionCategory.None;
            _type_ball = Type.PARTICULE;
        }


        public bool isDead
        {
            get { return _isDead; }
        }

        public float diameter
        {
            get { return radius * 2f; }
        }

        public float radius
        {
            get { return Geom.LocalVertices[0].X; }
        }

        public Type type
        {
            get { return _type_ball; }
        }

        public override void Initialize()
        {
            _sprite = ((MyGame)Game).Sprite;

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (_isDead) return;

            _time_elapsed += (int)(gameTime.ElapsedGameTime.Milliseconds * ((MyGame)Game).TimeValue);

            if (_time_elapsed >= _time_life) _isDead = true;

        }



        public override void Draw(GameTime gameTime)
        {
            //Obtenir la taille de l'image à afficher par rapport au zoom
            Rectangle rect = ((MyGame)Game).Camera.getRectangle(
                Geom.Body.Position.X,
                Geom.Body.Position.Y,
                diameter,
                diameter
            );

            //Si l'image a afficher est invisible on sort
            if (!((MyGame)Game).Camera.isVisible(rect)) return;

            _sprite.Draw
            (
                _txt2d_particule,
                rect,
                null,
                new Color(Color.White, (byte)(255f - 255f * ((float)_time_elapsed / (float)_time_life))),
                0, //Geom.Body.Rotation,
                new Vector2(_txt2d_particule.Width / 2f, _txt2d_particule.Height / 2f),
                SpriteEffects.None,
                0
            );
        }

        protected override void LoadContent()
        {
            _txt2d_particule = TexturesPool.GetTexture(_path_particule);
        }

        protected override void UnloadContent()
        {
        }
    }
}